<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="提醒内容" prop="content">
                <el-input v-model="queryParams.content" placeholder="请输入提醒内容" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="用户名称" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入电话号码" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item> -->

            <el-form-item label="类型" prop="noticeType">
                <el-select v-model="queryParams.noticeType" placeholder="提醒类型" clearable style="width: 200px">
                    <el-option v-for="dict in remind_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- 
            序号、提醒类型、提醒内容、提醒范围、提醒人、提醒时间、发送单位、发送人、创建时间、状态、操作人员、操作
             -->
            <el-table-column label="序号" align="center" type="index" width="100">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="提醒类型" align="center" prop="remindType">
                <template #default="scope">
                    <dict-tag :options="remind_status" :value="scope.row.remindType" />
                </template>
            </el-table-column>
            <el-table-column label="提醒内容" align="center" prop="content">
                <template #default="scope">
                    <span @click="showContent(scope.row)">{{ scope.row.content }}</span>
                </template>
            </el-table-column>


            <el-table-column label="提醒范围" width="170" align="center" prop="scope">
                <template #default="scope">
                    <dict-tag :options="scopeType" :value="scope.row.scope" />
                </template>
            </el-table-column>
            <el-table-column label="提醒人" show-overflow-tooltip align="center" prop="scope">
                <template #default="scope">
                    <el-button link type="primary" icon="avatar" @click="handleDetail(scope.row)">提醒人</el-button>
                </template>
            </el-table-column>
            <el-table-column label="提醒时间" align="center" prop="sendTime" />
            <el-table-column label="发送单位" align="center" prop="merchantName" />
            <el-table-column label="发送人" align="center" prop="userName" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :options="[
                        {
                            label: '正常',
                            value: '1'
                        },
                        {
                            label: '禁用',
                            value: '0'
                        }
                    ]" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="操作人员" align="center" prop="updateUserName" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改提醒对话框 -->
        <el-dialog :title="title" v-model="open" width="780px" append-to-body>
            <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="日期" prop="dateList">
                            <el-date-picker class="flex-1" v-model="form.dateList" type="dates" range-separator="至"
                                start-placeholder="开始日期" value-format="YYYY-MM-DD" end-placeholder="结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <!-- :is-range="true"  -->
                        <el-form-item label="时间" prop="timeList">
                            <el-time-select v-model="form.timeList" placeholder="选择时间">
                            </el-time-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="范围" prop="scope">
                            <el-select v-model="form.scope" placeholder="请选择">
                                <el-option v-for="dict in scopeType" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="类型" prop="remindType">
                            <el-select v-model="form.remindType" placeholder="请选择">
                                <el-option v-for="dict in remind_status" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="12">
                        <el-form-item label="接受人" prop="userIdList">
                            <el-select v-model="form.userIdList" placeholder="请选择" multiple>
                                <el-option v-for="dict in rangePeople" :key="dict.value" :label="dict.text"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="提醒内容">
                            <el-input type="textarea" v-model="form.content" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>



    </div>
</template>

<script setup name="Notice">
import {
    listRemind,
    delRemind,
    remindApiRemindAdd,
    remindApiInformationListReceiveUser
} from "@/api/backRemind/backRemind";

import {
    informationApiInformationListReceiveUser,
} from "@/api/backInformation/backInformation";

import { ElMessageBox } from 'element-plus'


import {
    getNotice,
    addNotice,
    updateNotice,
} from "@/api/system/notice";


const scopeType = [
    {
        value: "0",
        label: "今日就诊的患者和监护人",
    },
    {
        value: "1",
        label: "今日治疗的患者和监护人",
    },
    {
        value: "2",
        label: "今日入院的患者和监护人",
    },
    {
        value: "3",
        label: "今日出院的患者和监护人",
    },
    {
        value: "4",
        label: "今日住院的患者和监护人",
    },
    {
        value: "5",
        label: "家庭医生的签约患者和监护人",
    },
    {
        value: "6",
        label: "所有就诊过的患者和监护人",
    },
    {
        value: "7",
        label: "预约就诊的患者和监护人",
    },
    {
        value: "8",
        label: "预约治疗的患者和监护人",
    },
    {
        value: "9",
        label: "预约检查的患者和监护人",
    },
    {
        value: "10",
        label: "所有护士",
    },
    {
        value: "11",
        label: "所有医生",
    },
    {
        value: "12",
        label: "所有签约的家庭医生",
    },
    {
        value: "13",
        label: "所有行政人员",
    },
    {
        value: "14",
        label: "所有后勤人员",
    },
    {
        value: "15",
        label: "所有财务人员",
    },
    {
        value: "16",
        label: "所有采购人员",
    },
    {
        value: "17",
        label: "所有安保员工",
    },
    {
        value: "18",
        label: "所有全体员工",
    },
]



const rangePeople = [
    {
        value: "1741672611994300418",
        text: "陈开权",
        disabled: false,
    },
    {
        value: "1741014236189044737",
        text: "王丽",
        disabled: false,
    },
    {
        value: "1763098108078718978",
        text: "胡三十",
        disabled: false,
    },
    {
        value: "1767071768334483457",
        text: "呼呼呼",
        disabled: false,
    },
]

const { proxy } = getCurrentInstance();
const { remind_status } = proxy.useDict(
    "remind_status"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        content: undefined,
        phone: undefined,
        userName: undefined,
        type: undefined,
    },
    rules: {
    },
});

const { queryParams, form, rules } = toRefs(data);

const openDetail = ref(false)
const gridData = ref([])
/**详情按钮操作 */
function handleDetail(row) {
    reset();
    const dailyInformationId = row.dailyInformationId || ids.value;
    remindApiInformationListReceiveUser(dailyInformationId).then((response) => {
        console.log(response, "response");

        gridData.value = response;
        openDetail.value = true;
        title.value = "详情商家";
    });
}

/** 查询提醒列表 */
function getList() {
    loading.value = true;
    listRemind(queryParams.value).then((response) => {
        noticeList.value = response.records;
        total.value = response.total;
        loading.value = false;
    });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        scope: '',
        dailyInformationId: undefined,
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.dailyInformationId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加提醒";
}

/**修改按钮操作 */
function handleUpdate(row) {
    reset();
    const dailyInformationId = row.dailyInformationId || ids.value;
    getNotice(dailyInformationId).then((response) => {
        form.value = response;
        open.value = true;
        title.value = "修改提醒";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate((valid) => {
        if (valid) {
            if (form.value.dailyInformationId != undefined) {
                updateNotice(form.value).then((response) => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                form.value.timeList = [form.value.timeList]
                // form.value.userIds = [form.value.userIds] 
                // console.log(form.value)
                // return 
                remindApiRemindAdd(form.value).then((response) => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const dailyInformationId = row.dailyInformationId || ids.value;
    proxy.$modal
        .confirm('是否确认删除提醒编号为"' + dailyInformationId + '"的数据项？')
        .then(function () {
            return delRemind(dailyInformationId);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}

/** 查看内容 */
function showContent(row) {
    ElMessageBox.alert(row.content, '提醒内容', {
        confirmButtonText: '',
        showConfirmButton: false,
    })
}

getList();
</script>